<template>
  <div>
    <!--<BreakSpace label="查询条件" :containerStyle="{ marginBottom: '10px' }" />-->
    <!--<TopFilter  :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetFilter"></TopFilter>-->
    <BreakSpace label="查询列表" :containerStyle="{ marginBottom: '10px' }" />
    <!--<FilterTable-->
      <!--ref="table"-->
      <!--height="300"-->
      <!--:columns="columns"-->
      <!--:fetchapi="fetchApi"-->
      <!--:params="fetchParams"-->
      <!--:datakey="'records'"-->
      <!--:uidkey="'dealerCode'"-->
      <!--:defaultSelections="selectedRow"-->
      <!--:onSyncTableData="tableChangeFun"-->
      <!--:onRowSelectChange="selectChangeHandle"-->
      <!--:onPageChange="handleTablePageChange"-->
      <!--:onColumnsChange="columns => (this.columns = columns)"-->
    <!--/>-->
    <FilterTable
      ref="table"
      height="300"
      :columns="columns"
      :uidkey="'dealerCode'"
      :isMemoryPagination="isMemoryPagination"
      :dataSource="tableDealerList"
      :defaultSelections="selectedRow"
      :onSyncTableData="tableChangeFun"
      :onRowSelectChange="selectChangeHandle"
      :onPageChange="handleTablePageChange"
      :onSelectAll="selectAll"
      :onColumnsChange="columns => (this.columns = columns)"
    />
    <!--<BreakSpace label="已选清单" :containerStyle="{ marginBottom: '10px' }" />-->
    <!--<FilterTable ref="tables" height="auto" :dataSource="tableList" :columns="columns1" :onColumnsChange="columns => (this.columns1 = columns)" :isSelectColumn="false"> </FilterTable>-->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button @click="closeDrawer(2)" type="primary" :disabled="tableList.length < 1">确 定</el-button>
    </div>
  </div>
</template>
<script>
  import {dealerAttach, getDealerAll} from '@/api/omFinance'; //后台带分页接口
  import {dictionary} from '@/utils/dictMixin';
  import {arrayObjDeWeight} from '@/utils';
  import {isArcfox } from '@/api/cms-api/origin/commonDealerType';
  import { region } from '@/api/cms-common/region';
  export default {
  mixins: [dictionary,region],
  props: ['rowData'],

  data() {
    //this.tables = null;
    this.table = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      columns1: this.createdTables(),
      fetchParams: {},
      isMemoryPagination:true,
      appId: 'beijing',
      // fetchApi: dealerAttach,
      tableDealerList:[],
      backupData:[],
      selectedRow: [], // 选中的行项目
      totalArrList: [], // 选中的打印项目
      pageSelectRows: [], // 当前页选中的行项(过滤取消勾选项用)
      tableList: [],
      isSelect:false
    };
  },
  mounted() {
    this.table=this.$refs.table.$refs.pageTable;
    //this.tables = this.$refs.tables.$refs.pageTable;
    this.totalArrList = this.rowData; // 之前选中的经销商回显
    this.getDealerAllList();
  },
  methods: {
    selectAll(rowsData){
      if (!this.isSelect) {
        let rows=this.table.originData;
        rows.forEach(row => {
          this.table.toggleSelectionHandle(row, true)
        });
      } else {
        this.table.clearSelectionHandle();
      }
      this.isSelect=!this.isSelect
    },
   async getDealerAllList(){//currentPage=1&pageSize=20&pageNum=1&limit=20&current=1&size=20
     this.table.START_LOADING();
      // let res=await dealerAttach({currentPage:1,pageSize:22,pageNum:22,limit:22,current:1,size:22});
      let res=await getDealerAll();
     if (res.resultCode == 200) {
       this.table.STOP_LOADING();
        this.tableDealerList= res.data;
       // this.tableDealerList= res.data.records;


     }
    },
    // table数据变化
    tableChangeFun(val) {//val 所有数据
      //页面数据变化重新赋值选中行项
      this.selectedRow = val.filter(item => this.totalArrList.find(x => x.dealerCode == item.dealerCode));
      console.log(this.selectedRow, '页面数据变化重新赋值选中行项');
    },
    // table页码变化
    handleTablePageChange(e) {
      this.pageSelectRows = [];
    },
    // table行项选中变化
    selectChangeHandle(e) {
      // 跨页跨搜索逻辑
      // 取消选择的行项
      // let deleteRows = this.pageSelectRows.filter(item => !e.find(x => x.dealerCode == item.dealerCode));
      // console.log(deleteRows, '删除的行项');
      // // 当页选中行项
      // this.pageSelectRows = e;
      //
      // // 从总数据中删除取消勾选的行项
      // this.totalArrList = this.totalArrList.filter(item => !deleteRows.find(x => x.dealerCode == item.dealerCode));
      this.totalArrList = e.map(item=>this.table.originData.find(x=>x.dealerCode == item.dealerCode));
      console.log(this.totalArrList, 'totalArrList');
      // 把选中数据合并到处理后总数组
      let totalPartList = [...this.totalArrList, ...e];
      // 根据经销商代码去重
      this.totalArrList = arrayObjDeWeight(totalPartList, 'dealerCode');
      // 向选择列表赋值
      this.tableList = this.totalArrList;
     // this.selectedRow = this.tableDealerList.filter(item => this.totalArrList.find(x => x.dealerCode == item.dealerCode));

      console.log(this.totalArrList, '行项选中变化');
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '公司名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商简称',
          dataIndex: 'dealerShortname',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商类型',
          dataIndex: 'dealerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: isArcfox()?this.createDictList('2538'):this.createDictList('2538').filter(x=>{ return x.value!=25381004 && x.value!=25381005&& x.value!=25381003}), //25381004 25381005
          render: p => {
            return <span>{this.createDictText(p.row.dealerType, '2538')}</span>;
          }
        },
        {
          title: '经销商级别',
          dataIndex: 'shopLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2540'),
          render: p => {
            return <span>{this.createDictText(p.row.shopLevel, '2540')}</span>;
          }
        },//dealerType

        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '县区',
          dataIndex: 'districtName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createdTables() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    changeHandle(val) {
      this.getDealerAllList();
  // console.log("val====",val)
  //     if(val.dealerCode ===undefined && val.dealerName ===undefined) return;
  //     //let tempList =this.tableDealerList;
  //     if(val.dealerCode !==undefined) {
  //       this.tableDealerList = this.tableDealerList.filter(x => {
  //         if (val.dealerCode !== undefined && x.dealerCode.toLowerCase().includes(val.dealerCode.toString().toLowerCase())) {
  //           console.log("x.dealerCode.toLowerCase()", x.dealerCode.toLowerCase())
  //           console.log("val.dealerCode.toString().toLowerCase()", val.dealerCode.toString().toLowerCase())
  //           return x
  //         }
  //       });
  //     }
  //     if(val.dealerName !==undefined) {
  //       this.tableDealerList = this.tableDealerList.filter(x => {
  //         if (val.dealerName !== undefined && x.dealerName.includes(val.dealerName.toString())) {
  //           console.log("x.dealerName.====", x.dealerName)
  //           console.log("val.dealerName.toString().=====", val.dealerName)
  //           return x
  //         }
  //       })
  //     }
      // this.tableDealerList=tempList;
    },
    resetFilter(e){
     // this.$nextTick(()=>{{
     //   this.tableDealerList=Object.assign([],this.backupData);
     //   console.log("this.tableDealerList====", this.tableDealerList)
     // }})
    },

    delFun(data) {
      console.log('data====',data)
      let listData=this.table.originData.find(x=>x.dealerCode==data.dealerCode)
      this.table.toggleSelectionHandle(listData, false)
      //this.tables.EXECUTE_DELETE([data]);
    },
    closeDrawer(type) {
      if (type == 1) {
        this.$emit('close', false);
      } else {
        this.$emit('close', false, this.tableList);
      }
    }
  }
};
</script>
